package tmproject.hlhj.fhp.cartrans.fragments

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import kotlinx.android.synthetic.main.aty_regis.*
import kotlinx.android.synthetic.main.fgm_review.view.*
import tmproject.hlhj.fhp.cartrans.BaseFgm
import tmproject.hlhj.fhp.cartrans.IView.HomeView
import tmproject.hlhj.fhp.cartrans.IView.ReViewView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.activitys.ReviewDetaiAty
import tmproject.hlhj.fhp.cartrans.adapter.LearnListAdp
import tmproject.hlhj.fhp.cartrans.adapter.ReViewListAdp
import tmproject.hlhj.fhp.cartrans.javaBean.*
import tmproject.hlhj.fhp.cartrans.presenter.HomePresenter
import tmproject.hlhj.fhp.cartrans.presenter.LearnPresenter
import tmproject.hlhj.fhp.cartrans.presenter.ReViewPresenter
import tmproject.hlhj.fhp.cartrans.utils.SpHelper

/**
 * Created by Never Fear   on 2018\8\28 0028.
Never More....
 */
class ReViewFgm :BaseFgm<ReViewView,ReViewPresenter>(), ReViewView, HomeView, SpringView.OnFreshListener {
    private var classAdp:ReViewListAdp?=null
    private val datas=ArrayList<ReViewListBean.DataBean.ListBean>()
    private var page=1;
    private var hasMore=true
    private lateinit var homePresenter: HomePresenter
    override fun bindView(): ReViewView {
        return this
    }

    override fun createPresenter(): ReViewPresenter {
        homePresenter= HomePresenter()
        homePresenter.createModule()
        homePresenter.bindView(this)
        return  ReViewPresenter()
    }

    override fun getContentId(): Int {
        return R.layout.fgm_review
    }

    override fun initView() {


        classAdp= ReViewListAdp(datas)
        rootView.recyclerview.adapter=classAdp
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        rootView.spView.header=DefaultHeader(context)
        classAdp?.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                if(hasMore){
                    page++
                    presenter?.getLearnList(context!!,SpHelper.getToken(context!!),page)

                }else{

                }

            }
        })
        rootView.spView.setListener(this)
    }

    override fun onLoadmore() {
    }

    override fun onRefresh() {
        page=1
        datas.clear()
        presenter?.getLearnList(context!!,SpHelper.getToken(context!!),page)
        classAdp?.notifyDataSetChanged()
    }

    override fun setClassList(reViewListBean: ReViewListBean) {
        rootView.spView.onFinishFreshAndLoad()
        classAdp?.loadMoreComplete()
        datas.addAll(reViewListBean.data.list)
        classAdp?.notifyDataSetChanged()
    }

    override fun logicStart() {
        presenter?.getLearnList(context!!,SpHelper.getToken(context!!),page)
        presenter?.getExamTimes(SpHelper.getToken(context!!),context!!)
        homePresenter.getLearnInfo(context!!,SpHelper.getToken(context!!))
        homePresenter?.getInfo(context!!,SpHelper.getToken(context!!))
    }

    override fun setListener() {
        classAdp?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(context, ReviewDetaiAty::class.java)
            intent.putExtra("id",datas[position].id)
            startActivityForResult(intent,1)
            
        }
    }
    /**
     * 无效
     */
    override fun setDriverInfo(driverInfoBean: DriverInfoBean) {
        rootView.companyName.text=driverInfoBean.data.name
    }

    override fun setLearn(learInfoBean: LearInfoBean) {
        if (learInfoBean?.data.month_learned_time!=null){

            rootView.tvTime.text=learInfoBean?.data.month_learned_time+"分钟"
        }else{
            rootView.tvTime.text="0.0"+"分钟"
        }
    }

    override fun setUnReadNum(unReadNumBean: UnReadNumBean) {
    }

    override fun setExamTimes(examTImesBean: ExamTImesBean) {
        rootView.examNum.text="考试:${examTImesBean.data.count}次"

    }

    override fun noMore() {
        hasMore=false
        classAdp?.loadMoreEnd()
    }
}